package com.meida.module.bus.provider.api.config;

import cn.hutool.log.StaticLog;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BBpWbsarchitecture;
import com.meida.module.bus.client.entity.BmmdMaterial;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmMaterial;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmService;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmUnit;
import com.meida.module.bus.provider.api.util.PmsToolUtil;
import com.meida.module.bus.provider.service.BBpWbsarchitectureService;
import com.meida.module.bus.provider.service.BmmdMaterialService;
import com.meida.module.bus.provider.service.srmservice.SrmServiceService;
import com.meida.module.bus.provider.service.srmservice.SrmSqlserCommonService;
import com.meida.module.bus.provider.service.srmservice.SrmUnitService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
public class MaterialNoSyncCk {
    @Autowired
    private BBpWbsarchitectureService wbsarchitectureService;
    @Autowired
    private BmmdMaterialService bmmdMaterialService;
    @Autowired
    private SrmServiceService srmServiceService;
    @Autowired
    private SrmUnitService srmUnitService;
    @Autowired
    private SrmSqlserCommonService srmSqlserCommonService;

    /**
     * WBS存在性校验
     * @param wbs
     * @return
     */
//    @Async("ckSycnFuture")
    public CompletableFuture<List<String>> ckSycnFutureForWbs(List<String> wbs, String projectCode) {
        StaticLog.info("wbsCkSycnFuture-------start");
//        List<List<String>> wbsGroup = PmsToolUtil.fixedGrouping(wbsIn, 500);
        List<String> notExsist = Lists.newArrayList();
//        wbsGroup.stream().forEach(wbs->{
//            CriteriaQuery<BBpWbsarchitecture> wbsarchitectureCriteriaQuery = new CriteriaQuery(BBpWbsarchitecture.class);
//            wbsarchitectureCriteriaQuery.in("wbscode", wbs);
//            List<BBpWbsarchitecture> busMaterialList = wbsarchitectureService.list(wbsarchitectureCriteriaQuery);
//            wbs.stream().forEach(wbsStr->{
//                if(!busMaterialList.stream().anyMatch(busMaterialObj->busMaterialObj.getWbscode().equals(wbsStr))) {
//                    notExsist.add(wbsStr);
//                }
//            });
//        });

        Map<String, Object> wbsMap = Maps.newHashMap();
        wbsMap.put("projectCode", projectCode);
        Collection<BBpWbsarchitecture> bBpWbsarchitectures = wbsarchitectureService.listByMap(wbsMap);
        wbs.stream().forEach(wbsStr->{
            if(!bBpWbsarchitectures.stream().anyMatch(busMaterialObj->busMaterialObj.getWbscode().equals(wbsStr))) {
                notExsist.add(wbsStr);
            }
        });
        StaticLog.info("wbsCkSycnFuture-------end");
        return CompletableFuture.completedFuture(notExsist);
    }

    /**
     * 物料存在性校验
     * @param materialCodeS
     * @return
     */
//    @Async("ckSycnFuture")
    public CompletableFuture<List<String>> ckSycnFutureForMaterial(List<String> materialCodeS) {
        StaticLog.info("CkSycnFutureForMaterial-------start");
//        List<List<String>> materialCodeGroup = PmsToolUtil.fixedGrouping(materialCodeSIn, 500);
        List<String> notExsist = Lists.newArrayList();
//        materialCodeGroup.stream().forEach(materialCodeS->{
//            CriteriaQuery<BmmdMaterial> bmmdMaterialCriteriaQuery = new CriteriaQuery(BmmdMaterial.class);
//            bmmdMaterialCriteriaQuery.in("materialCode", materialCodeS);
//            List<BmmdMaterial> bmmdMaterialExists = bmmdMaterialService.list(bmmdMaterialCriteriaQuery);
//
//            materialCodeS.stream().forEach(materialCode->{
//                if(!bmmdMaterialExists.stream().anyMatch(bmmdObj->bmmdObj.getMaterialCode().equals(materialCode))) {
//                    notExsist.add(materialCode);
//                }
//            });
//        });

        Map<String, Object> materialMap = Maps.newHashMap();
        String materialCodeArray[] = new String[materialCodeS.size()];
        materialCodeS.toArray(materialCodeArray);
        materialMap.put("ids", materialCodeArray);
        List<SrmMaterial> bmmdMaterialExists = srmSqlserCommonService.getSrmMaterial(materialMap);
        materialCodeS.stream().forEach(materialCode->{
            if(!bmmdMaterialExists.stream().anyMatch(bmmdObj->bmmdObj.getMaterialCode().equals(materialCode))) {
                notExsist.add(materialCode);
            }
        });
        StaticLog.info("CkSycnFutureForMaterial-------end");
        return CompletableFuture.completedFuture(notExsist);
    }

    /**
     * 服务代码存在性校验
     * @param fbCodeList
     * @return
     */
    public List<String> ckSycnFutureForSrmServiceCode(List<String> fbCodeList) {
        StaticLog.info("ckSycnFutureForSrmServiceCode-------start");
        List<String> notExsist = Lists.newArrayList();
        CriteriaQuery<SrmService> srmServiceCriteriaQuery = new CriteriaQuery(SrmService.class);
        srmServiceCriteriaQuery.in("serviceCode", fbCodeList);
        List<SrmService> srmServiceList = srmServiceService.list(srmServiceCriteriaQuery);
        fbCodeList.stream().forEach(fbCode->{
            if(!srmServiceList.stream().anyMatch(srmServiceObj->srmServiceObj.getServiceCode().equals(fbCode))) {
                notExsist.add(fbCode);
            }
        });
        StaticLog.info("ckSycnFutureForSrmServiceCode-------end");
        return notExsist;
    }

    /**
     * 单位存在性校验
     * @param units
     * @return
     */
    public List<String> ckSycnFutureForUnit(List<String> units) {
        StaticLog.info("ckSycnFutureForUnit-------start");
        List<String> notExsist = Lists.newArrayList();
        CriteriaQuery<SrmUnit> srmUnitCriteriaQuery = new CriteriaQuery(SrmUnit.class);
        srmUnitCriteriaQuery.in("unitName", units);
        List<SrmUnit> srmUnitFilter = srmUnitService.list(srmUnitCriteriaQuery);

        CriteriaQuery<SrmUnit> srmUnitCodeCriteriaQuery = new CriteriaQuery(SrmUnit.class);
        srmUnitCodeCriteriaQuery.in("unitCode", units);
        List<SrmUnit> srmUnitFilterForCode = srmUnitService.list(srmUnitCodeCriteriaQuery);

        units.stream().forEach(unitStr->{
            if(!srmUnitFilter.stream().anyMatch(
                srmUniFlterObj->
                    srmUniFlterObj.getUnitName().equals(unitStr)
                )
                &&
                !srmUnitFilterForCode.stream().anyMatch(
                    srmUniFlterObj->
                        srmUniFlterObj.getUnitCode().equals(unitStr)
                )
            ) {
                notExsist.add(unitStr);
            }
        });
        StaticLog.info("ckSycnFutureForUnit-------end");
        return notExsist;
    }

}
